
//
//  SelectGroupController.m
//  Rail
//
//  Created by huyuchen on 2018/3/20.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "SelectGroupController.h"
#import "SelectGroupTableViewCell.h"

@interface SelectGroupController ()

@end

@implementation SelectGroupController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem addSimpleTitleView:@"选择分栏"];
    [self.tableView.mj_header beginRefreshing];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)refreshData {
    BMWeakSelf
    [HttpRequest loadFavGroupListDataSuccess:^(NSArray *object) {
        FavGroupModel *defaultModel = [FavGroupModel new];
        defaultModel.dir_name = @"默认分栏";
        defaultModel.dirid = @"0";
        weakSelf.dataArray = [NSMutableArray arrayWithObject:defaultModel];
        if ([object isKindOfClass:[NSArray class]]) {
            [weakSelf.dataArray addObjectsFromArray:object];
        }
        [weakSelf.tableView reloadData];
        [weakSelf.tableView.mj_header endRefreshing];
    } failure:^(NSString *code, NSString *msg) {
        [weakSelf.tableView.mj_header endRefreshing];
        Show_Msg_Error(msg)
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FavGroupModel *model = [self.dataArray objectAtIndex:indexPath.row];
    SelectGroupTableViewCell *cell = [SelectGroupTableViewCell cellWithInitStyleTableView:tableView class:[SelectGroupTableViewCell class]];
    cell.title = model.dir_name?:@"";
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50.f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    FavGroupModel *model = [self.dataArray objectAtIndex:indexPath.row];
    [self moveFavWithModel:model indexPath:indexPath];
}

//移动收藏栏目
- (void)moveFavWithModel:(FavGroupModel *)model indexPath:(NSIndexPath *)indexPath {
    Show_ActionEnabled
    Show_NoMsg
    BMWeakSelf
    [HttpRequest moveFavGroupWithFavId:self.favId groupId:model.dirid success:^(id object) {
        Show_Msg_Success(@"移动栏目成功")
        if (weakSelf.BlockSuccessCallBack) {
            weakSelf.BlockSuccessCallBack();
        }
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.7 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            Show_ActionDisabled
            [weakSelf.navigationController popViewControllerAnimated:YES];
        });
    } failure:^(NSString *code, NSString *msg) {
        Show_Msg_Error(msg)
        Show_ActionDisabled
    }];
}

@end
